﻿package com.projects.xenia.database_classes;

import java.io.File;

import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.projects.xenia.global_modules.MainGlobalModule;


import android.content.ContentValues;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class XmlIntoSql {

	public boolean xmlUpdate = false;
	public int CharactersCount;
	public Handler lHandler;
	public Message msg;		
	
	public void XmlParseAndInsert (File xFile, Handler xHandler) throws Exception {
		
		lHandler = xHandler;
		
		FileInputStream xInputStream =  new FileInputStream(xFile);
		
		SAXParserFactory factory = SAXParserFactory.newInstance();	
		SAXParser parser = factory.newSAXParser();		 
        XmlParseHandler handler = new XmlParseHandler();        
        parser.parse(xInputStream, handler);        
        
        if (MainGlobalModule.DBE.inTransaction()) {        	
        	MainGlobalModule.DBE.setTransactionSuccessful();
        	MainGlobalModule.DBE.endTransaction();
        }
        
        xInputStream.close();
                
	}

	public class XmlParseHandler extends DefaultHandler {	
					
		private int RowsCount;
		private String xDataTable;
		private ContentValues InsertValues;

			
	    @Override
	    public void endElement(String uri, String localName, String name)
	            throws SAXException {
	        super.endElement(uri, localName, name);
	        
	        if (RowsCount==0) {
	        	if (MainGlobalModule.DBE.inTransaction()) {
	        		MainGlobalModule.DBE.setTransactionSuccessful();
	            	MainGlobalModule.DBE.endTransaction();
	        	}
	        	MainGlobalModule.DBE.beginTransaction();
	        }
	        
	        if (localName.equalsIgnoreCase("row"))
	        {		
	        		if (xmlUpdate){
	        			
	        			long a;
	        			a = MainGlobalModule.DBE.update(xDataTable, InsertValues, "id=?",new String []{InsertValues.get("id").toString()});
	        			a = MainGlobalModule.DBE.insert(xDataTable, null, InsertValues);
	        			a = a+1;
	        		}
	        		else
	        		{
	        			MainGlobalModule.DBE.insert(xDataTable, null, InsertValues);
	        		}	        		
	        		RowsCount++;	        		
	        		if (RowsCount==1000) {
	        			MainGlobalModule.DBE.setTransactionSuccessful();
	    	        	MainGlobalModule.DBE.endTransaction();	    	        	
	    	        	RowsCount = 0;
	    	        }
	        }        	
	       
	    }

	    @Override
	    public void startDocument() throws SAXException {
	        super.startDocument();	        
	    }

	    @Override
	    public void startElement(String uri, String localName, String name,Attributes attributes) throws SAXException {
	        super.startElement(uri, localName, name, attributes);	                
	        if (name.equalsIgnoreCase("row")==true)
	        {	
	        	InsertValues = new ContentValues();
		        for (int i = 0; i < attributes.getLength(); i++) {		        	
		        	InsertValues.put(attributes.getLocalName(i), attributes.getValue(i));		        	
		        	}
	        }
	        
		        else if (name.equalsIgnoreCase("root")==false) {
		        	xDataTable = name;
		        	
		        	Bundle msg_data = new Bundle ();
					msg_data.putString("MessageValue", "Загрузка таблицы-"+xDataTable);
					msg_data.putBoolean("IndeterminateValue", true);				
					msg = lHandler.obtainMessage();
					msg.arg1 = 2;
					msg.arg2 = 0;
					msg.setData(msg_data);
					lHandler.sendMessage(msg);
		        	
		        	if (xmlUpdate==false){
		        		MainGlobalModule.DBE.delete(xDataTable,null,null);
		        	}
		        	
		        	if (MainGlobalModule.DBE.inTransaction()) {
		            	MainGlobalModule.DBE.setTransactionSuccessful();
		            	MainGlobalModule.DBE.endTransaction();
		            }
		        	
		        	RowsCount = 0;
		        	
		        }
	    }
	}
		
}


